package com.colorlight.cloud.complete;

/**
 * @author Demon
 * @description TODO
 * @date 28/5/2024  下午3:59
 */


import com.colorlight.cloud.complete.entity.enumerate.RealTimeMode;
import com.colorlight.cloud.complete.entity.enumerate.ScheduleTypeInnerStruct;
import com.colorlight.cloud.complete.entity.instructions.SetCommandSchedule;
import com.colorlight.cloud.complete.entity.po.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.util.*;

/**
 * @author john.lyle
 * @className com.colorlight.cloudwisdom.command.domain.dto AdvancedControlScheduleConvertDto
 * @date 2023/12/15 14:53
 * @description 高级控制指令排程转换参数
 */
@Data
public class AdvancedControlScheduleConvertDto {
	
	private static final long serialVersionUID = -6740814572182392434L;
	
	/**
	 * 显示屏唯一编码
	 */
	private String screenUuid;
	
	/**
	 * 高级控制任务id
	 */
	private Long advancedTaskId;
	
	/**
	 * 指令排程记录信息
	 */
	private List<CommandScheduleDto> commandScheduleList;
	
	@Data
	public static class CommandScheduleDto implements Serializable {
		
		private static final long serialVersionUID = 1132260382779096904L;
		
		/**
		 * 显示屏唯一编码
		 */
		private String screenUuid;
		
		/**
		 * 高级控制记录id
		 */
		private Long controlId;
		
		/**
		 * 指令id
		 */
		private Long commandId;
		
		/**
		 * 日期模式
		 */
		private RealTimeMode timeMode;
		
		/**
		 * 有效星期
		 */
		private Integer validWeek;
		
		/**
		 * 指令类型
		 */
		private ScheduleTypeInnerStruct scheduleTypeStruct;
		
		/**
		 * 具体指令字符串
		 */
		private String commandJson;
		
		/**
		 * 排程时间段（包含日期）
		 */
		private List<ScheduleLimitDate> limitDateList;
		
		/**
		 * 排程时间段（不包含日期）
		 */
		private List<ScheduleLimitTime> limitTimeList;
	}
	
	
	/**
	 * 排程时间段
	 */
	@Data
	@EqualsAndHashCode(callSuper = true)
	public static class ScheduleLimitDate extends ScheduleLimitTime implements Serializable {
		
		private static final long serialVersionUID = -627734126018357093L;
		
		/**
		 * 起始时间，包含日期
		 */
		private String startDate;
		
		/**
		 * 截止时间，包含日期
		 */
		private String endDate;
	}
	
	/**
	 * 周期排程时间段（不包含日期）
	 */
	@Data
	public static class ScheduleLimitTime implements Serializable {
		
		private static final long serialVersionUID = -1344273559344234484L;
		
		/**
		 * 起始时间，不包含日期
		 */
		private String startTime;
		
		/**
		 * 截止时间，不包含日期
		 */
		private String endTime;
	}
	
	public SetCommandSchedule organizationToCompleteCommand() {
		// 显示屏指令排程信息集合
		List<CommandSchedule> scheduleList = new LinkedList<>();
		// 显示屏指令排程主记录
		ScreenCommandSchedule screenCommandSchedule =
				new ScreenCommandSchedule();
		// 高级控制指令排程参数信息
		List<CommandScheduleDto> commandScheduleDtoList = this.getCommandScheduleList();
		screenCommandSchedule.setSeq(this.getAdvancedTaskId());
		screenCommandSchedule.setUuid(this.getScreenUuid());
		if (CollectionUtils.isEmpty(commandScheduleDtoList)) {
			screenCommandSchedule.setCommandSchedule(Collections.emptyList());
		} else {
			for (CommandScheduleDto scheduleConvertDto : commandScheduleDtoList) {
				CommandSchedule commandSchedule = new CommandSchedule();
				commandSchedule.setSeq(scheduleConvertDto.getControlId());
				commandSchedule.setCommandSeq(scheduleConvertDto.getCommandId());
				// 排程指令内容
				commandSchedule.setScheduleActions(scheduleConvertDto.getCommandJson());
				commandSchedule.setType(scheduleConvertDto.getScheduleTypeStruct());
				commandSchedule.setMode(scheduleConvertDto.getTimeMode());
				commandSchedule.setUuid(scheduleConvertDto.getScreenUuid());
				commandSchedule.setWeekday(scheduleConvertDto.getValidWeek());
				// 排程时间段
				commandSchedule.setLimitDate(getLimitDataList(scheduleConvertDto.getLimitDateList()));
				// 排程时间段（不包含时间）
				commandSchedule.setLimitTime(getLimitTimeList(scheduleConvertDto.getLimitTimeList()));
				scheduleList.add(commandSchedule);
			}
		}
		screenCommandSchedule.setCommandSchedule(scheduleList);
		// 排程指令输入信息
		SetCommandSchedule.InputMsg inputMsg =
				new SetCommandSchedule.InputMsg();
		inputMsg.setScreenCommandSchedule(Collections.singletonList(screenCommandSchedule));
		// 排程指令
		SetCommandSchedule setCommandSchedule
				= new SetCommandSchedule();
		setCommandSchedule.setInputMsg(inputMsg);
		return setCommandSchedule;
	}
	
	private List<LimitTime> getLimitTimeList(List<ScheduleLimitTime> limitTimeList) {
		if (CollectionUtils.isEmpty(limitTimeList)) {
			return Collections.emptyList();
		}
		List<LimitTime> timeList = new LinkedList<>();
		for (ScheduleLimitTime scheduleLimitTime : limitTimeList) {
			LimitTime limitTime = new LimitTime();
			limitTime.setStartTime(scheduleLimitTime.getStartTime());
			limitTime.setEndTime(scheduleLimitTime.getEndTime());
			timeList.add(limitTime);
		}
		timeList.sort(Comparator.comparing(LimitTime::getStartTime));
		return timeList;
	}
	
	private List<LimitDate> getLimitDataList(List<ScheduleLimitDate> limitDateList) {
		if (CollectionUtils.isEmpty(limitDateList)) {
			return Collections.emptyList();
		}
		List<LimitDate> dateList = new LinkedList<>();
		for (ScheduleLimitDate scheduleLimitDate : limitDateList) {
			LimitDate limitDate = new LimitDate();
			limitDate.setStartDate(scheduleLimitDate.getStartDate());
			limitDate.setStartTime(scheduleLimitDate.getStartTime());
			limitDate.setEndDate(scheduleLimitDate.getEndDate());
			limitDate.setEndTime(scheduleLimitDate.getEndTime());
			dateList.add(limitDate);
		}
		dateList.sort(Comparator.comparing(LimitDate::getStartDate));
		return dateList;
	}
}
